﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Text;

public partial class test_Default2 : System.Web.UI.Page
{

     string s_where, s_order;
        string curindex = String.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        Session["sTittle"] = "储值";
        if (!IsPostBack)
        {
            Drop_DataBrid();
            DataBrid();
        }
    }

    private void Drop_DataBrid()
    {
        //string sqlText = "select max(id) as id,[jigou] from account group by [jigou]";
        string sqlText = "SELECT DISTINCT [jigou] FROM [account]";
        this.DropDownList1.DataSource = DbHelper.ExecuteTable(CommandType.Text, sqlText, null);
        this.DropDownList1.DataBind();
        
    }


    // 李红新更改 2010-09-17 绑定数据
    private void DataBrid()
    {
        int record = 0;
        int count = 0;
        s_order = "ORDER BY [id] DESC";
        s_where = " where [role] ='学员'";
        string sqlText = "[id], [username], [username1], [gzdw], [chuzhi], [yingskf], [yiskf], [qskf]";
        DataTable DT = DbHelper.ExecutePage(sqlText, "[account]" + s_where, "id", s_order, Pager.CurrentPageIndex, Pager.PageSize, out record, out count, null);
        Pager.RecordCount = record;
        this.GridView1.DataSource = DT;
        this.GridView1.DataBind();
    }



    //李红新更改 2010-09-17 条件绑定数据
    private void DataBrid(string sqlwhere)
    {
        int record = 0;
        int count = 0;
        s_order = "ORDER BY [id] DESC";
        //s_order = "ORDER BY [xtsj2] DESC";
        string sqlText = "[id], [username], [username1], [gzdw], [chuzhi], [yingskf], [yiskf], [qskf]";
        DataTable DT = DbHelper.ExecutePage(sqlText, "[account]" + sqlwhere, "id", s_order, Pager.CurrentPageIndex, Pager.PageSize, out record, out count, null);
        Pager.RecordCount = record;
        this.GridView1.DataSource = DT;
        this.GridView1.DataBind();
    }





    protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
    {
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
            if (CheckBox2.Checked == true)
            {
                cbox.Checked = true;
                            }
            else
            {
                cbox.Checked = false;
            }
        }
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex != -1)
        {
            e.Row.Cells[0].Text = (e.Row.RowIndex + (Pager.PageSize * (Pager.CurrentPageIndex - 1) + 1)).ToString();
        }
    }
  
    protected void Button2_Click(object sender, EventArgs e)
    {
        
        int j = 0;
        var accessPeson = new StringBuilder();
        var failPeson = new StringBuilder();
        string info = string.Empty;
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
            if (cbox.Checked == true)
            {
                j = j + 1;
                string zid = GridView1.Rows[i].Cells[2].Text;         //e.Row.Cells[0].Text
                string zusername=GridView1.Rows[i].Cells[3].Text;   //e.Row.Cells[0].Text
                string zczje=TextBox1.Text;
                if (this.ExeSql(zid,zusername,zczje)==1)
                {
                    accessPeson.AppendFormat("{0},",zusername);
                }
                else
                {
                    failPeson.AppendFormat("{0},", zusername);
                }
            }
        }
        if (j == 0)
        {
            Label2.Text = "未选择用户";
        }
        else
        {
            if (accessPeson.Length > 0)
            {
                info += "储值成功的用户:" + accessPeson.ToString();
            }
            if (failPeson.Length > 0)
            {
                info += "储值失败的用户:" + failPeson.ToString();
            }
            ClientScript.RegisterStartupScript(GetType(), "infomation", "<script>alert('" + info + "')</script>");
            DataBrid();
        }
        CheckBox2.Checked = false;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string zsql = "";
        string zusername=TextBox2.Text.ToString().Trim();
        string zjigou=DropDownList1.SelectedValue.ToString().Trim();
        Pager.CurrentPageIndex = 0;
        if (zusername == "")    //当平台管理员没有输入 用户名或用户姓名时
        {
           
            DataBrid(" WHERE ((jigou='"+zjigou+"' ) AND ([role] ='学员'))");
            //zsql="SELECT [id], [username], [username1], [gzdw], [chuzhi], [yingskf], [yiskf], [qskf] FROM [account]  ";
        }
        else     //平台管理员输入了姓名或用户名
        {
            if (zjigou == "请选择")
            {
                DataBrid(" WHERE (([username] like '%" + zusername + "%') OR ([username1] like '%" + zusername + "%') AND ([role] ='学员'))");
            }
            else
            {
                DataBrid(" WHERE ((jigou='" + zjigou + "' ) AND (([username] like '%" + zusername + "%') OR ([username1] like '%" + zusername + "%')) AND ([role] ='学员'))");
            }
           
            //zsql="SELECT [id], [username], [username1], [gzdw], [chuzhi], [yingskf], [yiskf], [qskf] FROM [account]  WHERE ((jigou='"+zjigou+"' ) AND (([username] like '%"+zusername+"%') OR ([username1] like '%"+zusername+"%')) AND ([role] ='学员'))";
        }
    }
    public int ExeSql(string zid, string zusername, string zczje)
    {
        DateTime dxtsj1 = DateTime.Now;
        //参数：zid，account表中的id，zusername，account表中的用户名
        //sql1为修改用户表中的储值额
        //sql2为想chongzhi表添加一条记录，记载该用户的何时充值及充值金额
        //用convert函数进行类型转换：[chuzhi]=convert(money,@chuzhi)
        string Sql1 = "update account set [chuzhi]=[chuzhi]+convert(money," + zczje + ") where id=convert(int," + zid + ")";
        //insert into kecheng (fenlei1,fenlei2,fenlei3,name,jianjie,writer,laoshi,jigou,xtsj1) values ('" + cfenlei1 + "','" + cfenlei2 + "','" + cfenlei3 + "','" + cname + "','" + cjianjie + "','" + cwriter + "','" + claoshi + "','" + cjigou + "','" + dxtsj1 + "')";
        string Sql2 = "insert into chongzhi (username,czje,czsj) values ('" + zusername + "',convert(money," + zczje + "),'" + dxtsj1 + "')";
        string myConnectionString = ConfigurationSettings.AppSettings["gConnectionString"];
        SqlConnection myConnection = new SqlConnection(myConnectionString);
        SqlCommand myCommand1 = new SqlCommand(Sql1, myConnection);
        SqlCommand myCommand2 = new SqlCommand(Sql2, myConnection);
        try
        {
            myConnection.Open();
            myCommand1.ExecuteNonQuery();
            myCommand1.Dispose();
            myCommand2.ExecuteNonQuery();
            myCommand2.Dispose();
            myConnection.Close();
            return 1;
        }
        catch
        {
            myConnection.Close();
            return 0;
        }
    }


    protected void pager_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
    {
        Pager.CurrentPageIndex = e.NewPageIndex;
        if (ViewState["sqlwhere"] != null)
        {
            DataBrid(ViewState["sqlwhere"].ToString());
        }
        else
        {
            DataBrid();
        }


    }


    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataBrid(string.Format(" where jigou = '{0}'",this.DropDownList1.SelectedValue));
        ViewState["sqlwhere"] = string.Format(" where jigou = '{0}'", this.DropDownList1.SelectedValue);
    }
}

